﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace Simhopp.Common
{
    public static class ReadCompetition
    {
        private static List<Competition> allCompetitions;
        private static List<Credentials> allCredentials;
        private static List<Diver> allDivers;
        private static List<Dive> allDives;
        private static List<Score> allScores;
        private static List<User> allUsers;



        /// <summary>
        /// Loads all the competitions from the database
        /// </summary>
        /// <returns>A list of type competition</returns>
        public static List<Competition> GetAllCompetitions()
        {
            allCompetitions = CompetitionMap.ReadAllCompetitionsFromDatabase();
            allCredentials = CredentialsMap.ReadAllCredentialsFromDataBase();
            allDivers = DiverMap.ReadAllDiversFromDatabase();
            allDives = DiveMap.ReadAllDivesFromDatabase();
            allScores = ScoreMap.ReadAllScoresFromDatabase();
            allUsers = UserMap.ReadAllUsersFromDataBase();

            foreach (var diver in allDivers)
            {
                var diver1 = diver;
                foreach (var dive in allDives.Where(dive => dive.DiverId == diver1.Id))
                    diver.AddDive(dive);
                diver.Credentials = (from credentials in allCredentials
                                     where credentials.Id == diver.CredentialsId
                                     select credentials).FirstOrDefault();
            }

            foreach (var user in from user in allUsers let user1 = user select user)
            {
                var tmpCredentials = new Credentials();
                foreach (var credentials in allCredentials)
                {
                    if (credentials.Id == user.CredentialsId)
                    {
                        tmpCredentials = credentials;
                        break;
                    }
                }
                user.Credentials = tmpCredentials;
            }

            foreach (var score in allScores)
            {
                score.Judge = (from judge in allUsers
                                      where judge.Id == score.JudgeId
                                      select judge).FirstOrDefault();
            }

            foreach (var dive in allDives)
            {
                dive.RemoveAllScores();
                var dive1 = dive;
                foreach (var score in allScores.Where(score => score.DiveId == dive1.Id))
                    dive.AddScore(score);
                dive.DiveType = Competition.DivingTypeList[dive1.KeyId];
            }

            if (allCompetitions.Count == 0)
            {
                var competition = new Competition();
                foreach (var user in allUsers.Where(user => user.AccountType == AccountTypes.Administrator))
                    competition.AddJudge(user);
                allCompetitions = new List<Competition>{competition};
                return allCompetitions;
            }

            foreach (var competition in allCompetitions)
            {
                var competition1 = competition;
                foreach (var diver in allDivers.Where(diver => diver.CompetitionId == competition1.Id))
                    competition.AddParticipant(diver);
                foreach (var user in allUsers.Where(user => user.CompetitionId == competition1.Id))
                    competition.AddJudge(user);
                foreach (var user in allUsers.Where(user => user.AccountType == AccountTypes.Administrator))
                {
                    if(!competition.Judges.Contains(user))
                        competition.AddJudge(user);
                }
            }
            return allCompetitions;
        }


        public static Competition ReadCompetitionById(int id)
        {
            GetAllCompetitions();
            foreach (Competition competition in allCompetitions)
                if (competition.Id == id) return competition;
            var emptyCompetition = new Competition();
            foreach (var user in allUsers.Where(user => user.AccountType == AccountTypes.Administrator))
                emptyCompetition.AddJudge(user);

            emptyCompetition.Name = "Default";
            return emptyCompetition;
        }



    }
}
